package team.star.werespos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.star.werespos.model.vo.ResultVO;
import team.star.werespos.model.entity.FoodCategory;
import team.star.werespos.service.FoodCategoryService;
import team.star.werespos.util.Result;

import java.util.List;

/**
 * FoodCategoryController class<br/>
 *
 * @author hdonghong
 * @since 2019/03/23
 */
@Slf4j
@RestController
@RequestMapping("/food/category")
public class FoodCategoryController {

    @Autowired
    private FoodCategoryService foodCategoryService;

    /**
     * 创建商品类别，json格式，要求传递 categoryName
     * 要求前端传
     * @param foodCategory
     * @return
     */
    @PostMapping("/new")
    public ResultVO<?> insertOne(@RequestBody FoodCategory foodCategory) {
        foodCategoryService.insertOne(foodCategory);
        return Result.success();
    }

    /**
     * 获取商品类别列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<FoodCategory>> listCategories(){
        return Result.success(
                foodCategoryService.listCategory(true)
        );
    }

    /**
     * 获取商品类别列表
     * @return
     */
    @GetMapping("/list/admin")
    public ResultVO<List<FoodCategory>> listCategoriesByAdmin(){
        return Result.success(
                foodCategoryService.listCategory(false)
        );
    }

    /**
     * 修改商品类别，只能改 categoryName 和 categoryType
     * {categoryId} 表示指定的类别的id，前端传递时实际形如：/123/update
     */
    @PostMapping("/{categoryId}/update")
    public ResultVO<?> updateById(
            @PathVariable Integer categoryId,
            @RequestBody FoodCategory foodCategory) {
        foodCategory.setCategoryId(categoryId);
        foodCategoryService.updateById(foodCategory);
        return Result.success();
    }

    /**
     * 删除商品类别
     */
    @GetMapping("/{categoryId}/delete")
    public ResultVO<?> deleteById(@PathVariable Integer categoryId) {
        foodCategoryService.deleteById(
                new FoodCategory().setCategoryId(categoryId)
        );
        return Result.success();
    }
}
